import axios from 'axios'
import { Message, Loading } from 'element-ui'
// create an axios instance
const service = axios.create({
    baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
    // withCredentials: true, // send cookies when cross-domain requests
    timeout: 60000, // request timeout
})
let loading

// loading开始
function startLoading() {
    loading = Loading.service({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(255,255,255,0.3)'
    })
}
// loading结束
function endLoading() {
    loading.close()
}
let loadingCount = 0
export function startLoadingAddCount() {
    if (loadingCount === 0) {
        startLoading()
    }
    loadingCount++
}
export function endLoadingSubCount() {
    loadingCount > 0 && loadingCount--
    if (loadingCount === 0) {
        endLoading()
    }
}
// request interceptor
service.interceptors.request.use(
    (config) => {
        startLoadingAddCount()
        const fuserId = window.sessionStorage.getItem('fuserId3')
        if (['post', 'put', 'delete'].includes(config.method)) {
            config.data.fuserId = fuserId
        }
        /* 开发生产环境 */
        if (process.env.NODE_ENV === 'development') {
            config.url = '/api' + config.url
        } else {
            config.url = 'http://www.sangxings.com:8808/jc' + config.url
        }
        return config
    },
    (error) => {
        endLoadingSubCount()
        // do something with request error
        return Promise.reject(error)
    }
)

// response interceptor
service.interceptors.response.use(
    (response) => {
        endLoadingSubCount()
        const res = response.data
        if (res.code === 0) {
            return Promise.resolve(res)
        } else if (res.code === 2) {
            return Promise.resolve(res)
        } else if (res.code === 400) {
            return Promise.resolve(res)
        } else if (res.code === 1 || res.code === -1) {
            return Promise.resolve(res)
        } else {
            return Promise.resolve(res)
        }
        return Message({
            message: res.message || '网络异常，请重新尝试1',
            type: 'error',
            showClose: true,
            duration: 2 * 1000
        })
    },
    (error) => {
        endLoadingSubCount()
        if (error.response.status) {
            switch (error.response.status) {
                case 500:
                    Message({
                        message: '网络异常，请重新尝试',
                        type: 'error',
                        showClose: true,
                        duration: 2 * 1000
                    })
                    break
                case 404:
                    Message({
                        message: '网络请求不存在',
                        type: 'error',
                        showClose: true,
                        duration: 2 * 1000
                    })
                    break
                case 502:
                    Message({
                        message: '当前服务器不稳定，请稍后尝试！',
                        type: 'error',
                        showClose: true,
                        duration: 2 * 1000
                    })
                    break
                default:
                    Message({
                        message: error.response.data.message,
                        type: 'error',
                        showClose: true,
                        duration: 2 * 1000
                    })
            }
        }
        return Promise.reject(error.response)
    }
)

export default service
